package com.yl.chainStore.control.company

import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.company.CompanySmsModel
import com.yl.chainStore.entity.company.CompanySmsModelType
import com.yl.chainStore.pojo.vo.system.CompanySmsModelSelectVo
import com.yl.chainStore.services.IWebCompanyDeptService
import com.yl.chainStore.services.company.ICompanySmsModelTypeService
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("company/sms/model")
open class CompanySmsModelController(
	private val iCompanySmsModelTypeService: ICompanySmsModelTypeService,
	private val iWebCompanyDeptService: IWebCompanyDeptService
):BaseCtrl() {

	/**
	 * 获取短信模板列表
	 */
	@PostMapping("getTypeList")
	@Transactional
	open fun getTypeList():ResponseBody{
		return returnSuccess(iWebCompanyDeptService.getSmsModelTypeList(getContext().subCompanyId))
	}


	/**
	 * 添加模板类型
	 */
	@PostMapping("addType")
	@Transactional(rollbackFor = [Exception::class])
	open fun addType(@RequestBody type: CompanySmsModelType):ResponseBody{
		val addSmsModelType = iWebCompanyDeptService.addSmsModelType(type)

		if(addSmsModelType.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return addSmsModelType
		}
		return returnSuccess()
	}

	/**
	 * 修改类型
	 */
	@PostMapping("updateType")
	@Transactional(rollbackFor = [Exception::class])
	open fun updateType(@RequestBody type: CompanySmsModelType):ResponseBody{
		val updateType = iCompanySmsModelTypeService.updateType(type)
		if(updateType.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return updateType
		}
		return returnSuccess()
	}

	/**
	 * 删除类型
	 */
	@PostMapping("delType/{id}")
	@Transactional(rollbackFor = [Exception::class])
	open fun delType(@PathVariable id:Long):ResponseBody{
		val delType = iCompanySmsModelTypeService.delType(id)
		if(delType.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return delType
		}
		return returnSuccess()
	}

	/**
	 * 模板列表
	 */
	@PostMapping("getModelList")
	@Transactional
	open fun getModelList(@RequestBody vo: CompanySmsModelSelectVo):ResponseBody{
		vo.companyId = getContext().companyId
		vo.subCompanyId = getContext().subCompanyId
		return returnSuccess(iCompanySmsModelTypeService.getModelList(vo))
	}

	/**
	 * 添加模板
	 */
	@PostMapping("addModel")
	@Transactional(rollbackFor = [Exception::class])
	open fun addModel(@RequestBody model:CompanySmsModel):ResponseBody{
		val addModel = iCompanySmsModelTypeService.addModel(model)
		if(addModel.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return addModel
		}
		return returnSuccess()
	}

	/**
	 * 修改模板
	 */
	@PostMapping("updateModel")
	@Transactional(rollbackFor = [Exception::class])
	open fun updateModel(@RequestBody model:CompanySmsModel):ResponseBody{
		val updateModel = iCompanySmsModelTypeService.updateModel(model)
		if(updateModel.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return updateModel
		}
		return returnSuccess()
	}
	/**
	 * 删除模板
	 */
	@PostMapping("delModel/{id}")
	@Transactional(rollbackFor = [Exception::class])
	open fun delModel(@PathVariable id:Long):ResponseBody{
		val delModel = iCompanySmsModelTypeService.delModel(id)
		if(delModel.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return delModel
		}
		return returnSuccess()
	}






}
